package com.fh.wanandroid.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import androidx.core.content.ContextCompat;

import com.fh.wanandroid.BuildConfig;
import com.fh.wanandroid.R;
import com.fh.wanandroid.di.component.AppComponent;
import com.fh.wanandroid.di.component.DaggerAppComponent;
import com.fh.wanandroid.di.module.AppModule;
import com.scwang.smartrefresh.header.DeliveryHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;

import java.util.logging.Logger;

import javax.inject.Inject;

import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasAndroidInjector;

public class MyApplication  extends Application  implements HasAndroidInjector {


    private static volatile AppComponent appComponent;

    private static MyApplication instance;
//    private RefWatcher refWatcher;
    public static boolean isFirstRun = true;

    @Inject
    DispatchingAndroidInjector<Object> mAndroidInjector;


    //static 代码段可以防止内存泄露, 全局设置刷新头部及尾部，优先级最低
//    static {
//
//        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
//            //全局设置主题颜色
//            layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
//            //指定为Delivery Header，默认是贝塞尔雷达Header
//            return new DeliveryHeader(context);
//        });
//        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
//            //默认是 BallPulseFooter
//            return new BallPulseFooter(context).setAnimatingColor(ContextCompat.getColor(context, R.color.colorPrimary));
//        });
//    }

    public static synchronized MyApplication getInstance() {
        return instance;
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();



        instance = this;

        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(instance))
                .build();

        appComponent.inject(this);

//        initAppUpdate();


        initLogger();

        if (BuildConfig.DEBUG) {
//            Stetho.initializeWithDefaults(this);
        }

    }





    private void initLogger() {
        //DEBUG版本才打控制台log
//        if (BuildConfig.DEBUG) {
//            Logger.addLogAdapter(new AndroidLogAdapter(PrettyFormatStrategy.newBuilder().
//                    tag(getString(R.string.app_name)).build()));
//        }
//        //把log存到本地
//        Logger.addLogAdapter(new DiskLogAdapter(TxtFormatStrategy.newBuilder().
//                tag(getString(R.string.app_name)).build(getPackageName(), getString(R.string.app_name))));
    }



    public static synchronized AppComponent getAppComponent() {
        if (appComponent == null) {
            appComponent = DaggerAppComponent.builder()
                    .appModule(new AppModule(instance))
                    .build();
        }
        return appComponent;
    }


    @Override
    public AndroidInjector<Object> androidInjector() {
        return mAndroidInjector;
    }
}
